package com.woniuxy.mail.rabbit_demo.listener;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.mail.rabbit_demo.entity.Order;
import com.woniuxy.mail.rabbit_demo.service.EmailService;
import com.woniuxy.mail.rabbit_demo.service.OrderService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;

/**
 * @author fengbenpeng
 * @description: 主动取消订单
 * @date 2022/3/14 19:38
 */
public class HandcancelListener {
    @Autowired
    private EmailService emailService;

    @Autowired
    private OrderService orderService;
    /**
     * 监听某个队列的消息
     * @param message 接收到的消息
     */
    @RabbitListener(queues = "order_cancel_email_queue")//这里表示的是监听order_add_email_queue这个队列的消息
    public void myListener1(String message){
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Order order = objectMapper.readValue(message, Order.class);
            Context context = new Context();
            context.setVariable("order",order);
           orderService.deleteOrderById(order.getId());
            //sender表示的是发送者,accepter表示的是接收者
            emailService.sendEmail("取消订单成功",context,"1558788078@qq.com","1558788078@qq.com","email_cancle.html");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
